IF EXISTS(select * from sysobjects where name = 'PesquisaUsuariosAmizadesStatus' and xtype = 'P')
BEGIN
	DROP PROCEDURE DBO.PesquisaUsuariosAmizadesStatus
END
GO
CREATE PROCEDURE DBO.PesquisaUsuariosAmizadesStatus
	@ID bigint
as
BEGIN
	SELECT 
		* 
	FROM 
		Usuarios
	WHERE 
		id IN 
		(
			SELECT 
				id_usuario as Id 
			FROM 
				Amizades 
			WHERE 
			id_usuario_amigo = @ID
			AND status_amizade = 0
		) 
END
GO
IF EXISTS(select * from sysobjects where name = 'SalvaAmizadesResposta' and xtype = 'P')
BEGIN
	DROP PROCEDURE DBO.SalvaAmizadesResposta
END
GO
Create PROCEDURE [dbo].[SalvaAmizadesResposta] 
 ( 
	@id bigint = NULL, 
	@id_usuario bigint = NULL, 
	@id_usuario_amigo bigint = NULL, 
	@data_sistema datetime = NULL, 
	@situacao bit = NULL, 
	@status_amizade char = NULL, 
	@data_amizade datetime = NULL  
) 
AS  
  
	BEGIN  
	set @id = (select id from Amizades
				WHERE 
				id_usuario = @id_usuario
				and id_usuario_amigo = @id_usuario_amigo )
			
	if (@id > 0 and @id is not null)
		BEGIN			
			UPDATE Amizades SET  
				
				data_sistema = ISNULL(@data_sistema,data_sistema),  
				situacao = ISNULL(@situacao,situacao),  
				status_amizade = ISNULL(@status_amizade,status_amizade),  
				data_amizade = ISNULL(@data_amizade,data_amizade) 
				
			WHERE 
			
				id_usuario = @id_usuario
				and id_usuario_amigo = @id_usuario_amigo
		 END
	 ELSE
		BEGIN
			UPDATE Amizades SET  
				
				data_sistema = ISNULL(@data_sistema,data_sistema),  
				situacao = ISNULL(@situacao,situacao),  
				status_amizade = ISNULL(@status_amizade,status_amizade),  
				data_amizade = ISNULL(@data_amizade,data_amizade) 
				
			WHERE 
			
				id_usuario = @id_usuario_amigo
				and id_usuario_amigo = @id_usuario
		
		END
		select @id
	END    
GO